package com.generic.mybatis.dal.interceptor.automap.builder;

import com.generic.mybatis.dal.interceptor.automap.SqlBuilder;
import com.generic.mybatis.dal.interceptor.automap.TableMapper;
import com.generic.mybatis.dal.interceptor.automap.TableMapperHolder;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.MappedStatement;

import static com.generic.mybatis.dal.interceptor.automap.builder.SqlBuilderUtil.*;

/**
 * 根据条件查询 并 锁定记录
 *
 * @author wuaj
 */
public class FindLockBuilder implements SqlBuilder {
    @Override
    public String buildSql(MappedStatement mappedStatement, Class dtoClass, Object parameterObject) throws Exception {
        TableMapper tableMapper = TableMapperHolder.getTableMapper(dtoClass);
        SQL sql = buildSelectStatement(tableMapper);
        buildCritialStatement(tableMapper, sql, parameterObject, "array");
        replaceResultMap(mappedStatement, tableMapper.getResultMap());
        return sql.toString().concat(" for update");
    }
}
